package com.zs.zcerp.salary.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.zcerp.salary.entity.MachineDepts;
import com.zs.zcerp.salary.mapper.MachineDeptsMapper;
import com.zs.zcerp.salary.service.IMachineDeptsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DS("base")
public class MachineDeptsServiceImpl extends ServiceImpl<MachineDeptsMapper, MachineDepts> implements IMachineDeptsService {

    @Override
    public MachineDepts machineDepts(Integer machine) {
        return baseMapper.selectOne(new QueryWrapper<MachineDepts>().eq("machine", machine));
    }

    @Override
    public int saveDepts(Integer machine, JSONArray depts) {
        MachineDepts machineDepts = new MachineDepts();
        machineDepts.setMachine(machine);
        machineDepts.setDepts(JSON.toJSONString(depts));
        int result = 0;
        try {
            result = baseMapper.insert(machineDepts);
        } catch (Exception e) {
            result = baseMapper.update(machineDepts, new QueryWrapper<MachineDepts>().eq("machine", machine));
        }
        return result;
    }
}
